#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include <string.h>


unsigned CalculateLevel(char c1,char c2);

int main(int argc,char **argv)
{

	
	Stack stack;

	Stack optStack;

	char pInput[64]={0};

	char cc=0;
	
	int inputlen=0;

	int i;


        Item *pTmp=NULL;

	StackInit(&stack);
	StackInit(&optStack);


	void IteratorStack(Stack *pStack);


	if(argc<2)
	{
		
		printf("input the string\n");
		return 0;
	}

	inputlen=strlen(*(argv+1));


	for(i=0;i<inputlen;i++)
	{
	
		cc=*(*(argv+1)+i);
		switch(cc)
		{
		
			case '+':
			case '-':
			case '*':
			case '/':

				pTmp=(Item *)malloc(sizeof(Item));
                                pTmp->element=atoi(pInput);
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&stack); 
                                IteratorStack(&stack);
				pInput[0]=0;

				
				pTmp=(Item *)malloc(sizeof(Item));
                                pTmp->element=(int)cc;
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&optStack);			
				IteratorStack(&optStack);
				break;
			default:

				strncat(pInput,&cc,1);
				break;
		
		}
	}



	inputlen=strlen(*(argv+1));

	if(inputlen>0)
	{
	
			 	pTmp=(Item *)malloc(sizeof(Item));
                                pTmp->element=atoi(pInput);
                                pTmp->pNext=NULL;
                                StackPush(pTmp,&stack);
                                IteratorStack(&stack);
	}

	
		
	printf("%d",StackCalculate(&stack,&optStack));

	return 0;
}



 unsigned STACK_SIZE;
        unsigned CAPCITY;
        Item *item;



nodeval StackCalculate(Stack *pStack,Stack *optStack)
{


	int calNum1,calNum2,calResult;
        Item *opt=NULL,*pResult=NULL;
	
	char firstChar=0,nextChar=0;

        Item *pNumber1=NULL,*pNumber2=NULL;
	void IteratorStack(Stack *pStack);

	 while((opt=StackPop(optStack))!=NULL)
                {


			nextChar==0;
			calNum1=(pNumber1=StackPop(pStack))->element;


			if(optStack->STACK_SIZE>0)
			{
			
				 nextChar=(char)optStack->item->element;

			}

			firstChar=(char)opt->element;

			if(CalculateLevel(firstChar,nextChar)>0)
			{
				
				calNum2=StackCalculate(pStack,optStack);
			}else{
				
	                        calNum2=(pNumber2=StackPop(pStack))->element;
			}

                        switch(firstChar)
                        {
                                case '+':
                                        calResult=calNum2+calNum1;
                                        break;
                                case '-':
                                         calResult=calNum2-calNum1;
                                         break;
                                case '*':
                                         calResult=calNum2*calNum1;
                                         break;
                                case '/':
                                         calResult=calNum2/calNum1;
                                         break;

                        }
  	              printf("cal the result\n");


	                        pResult=(Item *)malloc(sizeof(Item));
                                pResult->element=calResult;
                                pResult->pNext=NULL;
                                StackPush(pResult,pStack);
                                IteratorStack(pStack);



                }


	 if(pStack==NULL)
	 {
	 	return 0;
	 }


	return 	 pStack->item->element;


}




unsigned CalculateLevel(char c1,char c2)
{

	if(c1=='+'||c1=='-')
	{
		if(c2=='*'||c2=='/')
		{
		
			return 1;
		}
		
	}

	return 0;

}


void StackInit(Stack *pStack)
{


	Item *pItem=NULL;
	if(pStack==NULL)
	{
		printf("the stack mem is NULL");
		return;
	}

	pStack->CAPCITY=8;
	pStack->STACK_SIZE=0;
	pStack->item=NULL;	
	


}





/**
 * push the item to stack
 */
void  StackPush(Item *item,Stack *pStack)
{

	Item *pTop=NULL;
	if(item==NULL)
	{
		return;
	}

	if(pStack==NULL)
	{
		return;
	}
	
	if(pStack->STACK_SIZE+1>=pStack->CAPCITY)
	{
		return;
	}


	pTop=pStack->item;

	if(pTop!=NULL)
	{
		item->pNext=pTop;
	}


	pStack->item=item;

	pStack->STACK_SIZE=pStack->STACK_SIZE+1;

}




void IteratorStack(Stack *pStack)
{

  	 Item *pItem=NULL;
         if(pStack==NULL)
         {
                return;
        }


        pItem=pStack->item;

        if(pItem==NULL)
        {

                pStack->STACK_SIZE=0;
                return;
        }

	printf("print the stack element\n\t");
	while(pItem!=NULL)
	{
	
	
		printf("%d\t",pItem->element);
		pItem=pItem->pNext;
	}

	printf("\n");

}


/**
 * pop the top element 
 */
Item *StackPop(Stack *pStack)
{

	 Item *pTop=NULL;
	 if(pStack==NULL)
	 {
                return NULL;
        }

	
	pTop=pStack->item;

	if(pTop==NULL)
	{
	
		pStack->STACK_SIZE=0;
		return NULL;
	}

	pStack->item=pTop->pNext;
	pStack->STACK_SIZE=pStack->STACK_SIZE-1;
	return pTop;	
}
